import dayjs from "dayjs";
import { PluginLunar } from "dayjs-plugin-lunar";
dayjs.extend(PluginLunar);
export const parseUserInfo = (userInfo: any) => {
    const { userProfileId, name, gender, solarBirthday = dayjs(), lunarBirthday, birthTime = dayjs(), solarTime, province, provinceCode, city, cityCode, area, areaCode, county, countyCode, street } = userInfo || {};
    const birthdayStr = `${dayjs(solarBirthday).format('YYYY-MM-DD')} ${dayjs(birthTime).format('HH:mm:ss')}`
    return {
        username: name || '',
        gender: gender || "0",
        birthday: parseBirthday(birthdayStr),
        address: provinceCode ? [provinceCode, cityCode, areaCode] : [],
        addressStr: provinceCode ? `${province}/${city}/${area}` : '',
        street: street || ''
    }
};

const parseBirthday = (birthday: string) => {
    if (!birthday) return new Date()
    return dayjs(birthday).toDate()
}

export const convertToUserInfo = (formInfo: any) => {
    const { username, gender, birthday, address, addressStr, street } = formInfo || {};
    const [province = "", city = "", area = ""] = addressStr?.split('/') || []
    const [provinceCode = "", cityCode = "", areaCode = ""] = address || []
    return {
        name: username || '',
        gender: gender || "0",
        solarBirthday: dayjs(birthday).format('YYYY-MM-DD') || '',
        birthTime: dayjs(birthday).format('HH:mm:ss') || '',
        provinceCode,
        cityCode,
        areaCode,
        lunarBirthday: dayjs(birthday).format('YYYY-MM-DD') || '',
        solarTime: dayjs(birthday).format('HH:mm:ss') || '',
        province,
        city,
        area,
        county: area,
        countyCode: areaCode,
        street: street || ''
    }
}